// package-store/pages/store-sns-edit/index.js
import { ManageTargetInfoUpdateMixins } from '../../../behaviors/page';
import { setTitle, navBack, showLoading, hideLoading, checkImageSecurity, tipToast } from '../../../common/env';
import { SNS_TYPE, SNS_TYPE_ENUM } from '../../../constants/common';
// import { uploadImage } from '../../../common/qiniu';
import { uploadImage } from '../../../common/cos';

Page({
    mixins: [ManageTargetInfoUpdateMixins],
    data: {
        snsType: SNS_TYPE,
        snsInfo: {},
        qrCodeList: [],
        viewTypeActionSheet: {
            show: false,
            list: [
                { name: '快捷跳转', value: 'path' },
                { name: '二维码识别', value: 'qrCode' },
            ],
            map: {
                path: { name: '快捷跳转', value: 'path' },
                qrCode: { name: '二维码识别', value: 'qrCode' },
            },
        }
    },
    onLoad(options) {
        const { type, snsId } = options;
        setTitle(SNS_TYPE_ENUM[type].name);
        if (snsId) {
            this._handleGetSnsInfo(warehouseId);
        } else {
            const snsInfo = {
                type,
                viewType: 'qrCode', 
            };
            this.setData({ snsInfo });
        }
    },
    async _handleGetSnsInfo(snsId) {
        showLoading('加载中...');
        const snsInfo = {};
        const qrCodeList = [];
        if (qrCodeList.qrCode) {
            qrCodeList.push({ url: qrCodeList.qrCode });
        }
        this.setData({
            snsInfo,
            qrCodeList
        });
        hideLoading();
    },
    handleShowViewTypeActionSheet() {
        this.setData({
            'viewTypeActionSheet.show': true,
        });
    },
    handleCloseViewTypeActionSheet() {
        this.setData({
            'viewTypeActionSheet.show': false,
        });
    },
    async handleUploadQrcode(e) {
        const { file } = e.detail;
        const flag = await checkImageSecurity(file.url);
        if (!flag) {
            tipToast('请确认图片合法、合规');
            return; 
        }
        const res = await uploadImage('qrcode', file.url);
        const qrCodeList = [{ url: res.url }];
        this.setData({
            qrCodeList,
            'snsInfo.qrCode': res.url,
        });
    },
    handleDeleteQrcode() {
        this.setData({
            qrCodeList: [],
            'snsInfo.qrCode': '',
        });
    },
    handleChange(e) {
        const { type } = e.currentTarget.dataset;
        const { snsInfo } = this.data;
        snsInfo[type] = e.detail;
        this.setData({ snsInfo });
    },
    handleChangeViewType(e) {
        const { value } = e.detail;
        this.setData({
            'snsInfo.viewType': value,
        });
    },
    handleSave() {
        const { snsInfo } = this.data;
        navBack();
    },
})
